package kr.co.hpc.partner.controller;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.ibatis.session.SqlSession;
import org.apache.log4j.Logger;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.context.support.HttpRequestHandlerServlet;
import org.springframework.web.servlet.ModelAndView;

import kr.co.hpc.partner.form.Contact;
import kr.co.hpc.partner.vo.UserVo;

@Controller
@SessionAttributes
public class LoginController extends SqlSessionDaoSupport {
	// get log4j handler
	private static final Logger logger = Logger
			.getLogger(LoginController.class);

	@Autowired
	private SqlSession sqlSession;

	// @RequestMapping(value = "/submitLogin", method = RequestMethod.GET)
	// public String showLogin(){
	// return "submitLogin";
	// }

	@RequestMapping(value = "/submitLogin", method = RequestMethod.POST)
	public @ResponseBody	ResponseEntity<String> submitLogin(@ModelAttribute("login") UserVo userVo,
			BindingResult result, HttpServletRequest request) {
		String rt = "";
		UserVo rvo = new UserVo();
		HttpSession session = request.getSession(false);

		rvo = (UserVo) this.sqlSession.selectOne("user.selectUser", userVo);

		if (rvo == null) {
			session.removeAttribute("userId");
			rt = "nomatch";
		} else {
			System.out.println("userName == " + rvo.getUserName().toString());
			session.setAttribute("userId", rvo.getUserId().toString());
			session.setAttribute("userName", rvo.getUserName().toString());
			logger.debug("getUserId = " + rvo.getUserId());
			logger.debug("getUserPw = " + rvo.getUserPw());
			logger.debug("getCompanyCd = " + rvo.getCompanyCd());
			logger.debug("getGroupNo = " + rvo.getGroupNo());
			logger.debug("getUserName = " + rvo.getUserName());

			ObjectMapper om = new ObjectMapper();
			try {
				rt = om.defaultPrettyPrintingWriter().writeValueAsString(rvo);
				// response.setHeader("Cache-Control", "no-cache");
				// response.setContentType("text/html;charset=utf-8");
				// response.getWriter().write(result);
				rt = "{\"result\":\"success\"," + rt + "}";
			} catch (JsonGenerationException e) {
				e.printStackTrace();
			} catch (JsonMappingException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
			logger.debug("json rt = " + rt);
		}
		HttpHeaders responseHeaders = new HttpHeaders();
		responseHeaders.add("Content-Type", "text/html; charset=UTF-8");
		return new ResponseEntity<String>(rt, responseHeaders, HttpStatus.CREATED);
//		return rt;
		// return "성공";
	}

	@RequestMapping("/result")
	public ModelAndView showResult() {

		return new ModelAndView("result", "command", new Contact());
	}

}
